package com.system.controller;

import com.system.entity.Question;
import com.system.entity.User;
import com.system.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("question")
public class QuestionController {

    @Autowired
    QuestionService questionService;

    /**
     * 跳转添加题目
     *
     */
    @RequestMapping(value="/toaddQuestion",method = RequestMethod.GET)
    public ModelAndView toaddQuestion(ModelAndView model){
        //通过ModelAndView中的方法跳转到userAdd.jsp页面
        model.setViewName("question/addQuestion");
        return model;
    }


    /**
     * 处理提交题目的form表单
     *
     */
    @RequestMapping(value="/addQuestion")
    public String addQuestion(Question question,Model model){
        try {
            questionService.addQuestion(question);
            model.addAttribute("message", "添加题目成功");
            return "question/addQuestion";

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 题目分页查询
     *
     * @param question 查询条件
     * @param pageCode 当前页
     * @param pageSize 每页显示的记录数
     * @return
     */
    @RequestMapping("/findByPage")
    public String findByPage(Question question,
                             @RequestParam(value = "pageCode", required = false, defaultValue = "1") int pageCode,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "4") int pageSize,
                             Model model) {
        // 回显数据

        model.addAttribute("page", questionService.findByPage(question, pageCode, pageSize));
        return "question/questionList";
    }

    /**
     * 删除题目
     *
     */
    @RequestMapping(value = "/deleteQuestion")
    public String deleteUser(@RequestParam("id") Integer id, Model model) {
        try {
            questionService.deleteQuestion(id);
            model.addAttribute("message", "删除题目信息成功");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "question/questionList";
    }
    /**
     * 通过id组批量删除题目
     * @param ids
     * @return
     */
    @RequestMapping("deleteByIds")
    @ResponseBody
    public String deleteQuestionByIds(Integer[] ids){
        try{
            //批量删除
//           System.out.println(ids.length) ;
//           for(int i=0;i<ids.length;i++){
//               System.out.println(ids[i]);
//           }
           questionService.deleteQuestionByIds(ids);
            return "ok";
        }
        catch (Exception e){
            return "error";
        }

    }

    /**
     * 通过id进行题目的编辑
     *
     */
    /**
     * 根据id查询用户信息方法
     *
     * @param question
     *  @return
     */

    @RequestMapping(value = "/findById",method = RequestMethod.POST)
    @ResponseBody
    public Question findById(@RequestBody Question question) {
        Question question_info = questionService.findById(question.getId());
        return  question_info;

    }

    /**
     * 更新用户信息的方法
     *
     * @param question
     * @param model
     * @return
     */
    @RequestMapping(value = "/updateQuestion")
    public String updateQuestion(Question question, Model model) {
        try {
            questionService.updateQuestion(question);
            model.addAttribute("message", "更新题目信息成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "question/questionList";
    }

}
